<?php
class Agent_IndexController extends Zend_Controller_Action
{
    public function indexAction ()
    {
        $authData = Zend_Auth::getInstance()->getIdentity();
    	$this->view->assign('id',$authData['identity']);
    	/*
        $agent = new Reale_Model_Users_Agent();
        $authData = Zend_Auth::getInstance()->getStorage()->read();
        $agent->setAgent_reg_id($authData['identity']);
        $info = $agent->getInfo();
        $this->view->assign('info', $info);*/
        
    }
    public function registerAction ()
    {
        // action body
    }
    public function imodAction ()
    {
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $this->_helper->layout()->disableLayout();
        $request = $this->getRequest();
        $params = array_diff($request->getParams(), $request->getUserParams());
        foreach ($params as $colName => $value) {
            $value = is_array($value) ? $value : htmlentities(trim($value));
            $params[$colName] = $value;
        }
        $father_name = $params['father_name'];
        $husband_name = $params['husband_name'];
        $mother_name = $params['mother_name'];
        $periodDateobj = new Zend_Date($params['dob'], 'dd-MM-yyyy');
        $params['dob'] = $periodDateobj->toString('YYYY-MM-dd');
        $dob = $params['dob'];
        $gender = $params['gender'];
        $nationality = $params['nationality'];
        $marital_status = $params['marital_status'];
        $pan = $params['pan'];
        $applicant_type = $params['applicant_type'];
        $occupation = $params['occupation'];
        $mobile_no = $params['mobile_no'];
        $alternate_no = $params['alternate_no'];
        $email = $params['email'];
        $ppostalcode = $params['ppostalcode'];
        $pstate = $params['pstate'];
        $pdistrict = $params['pdistrict'];
        $pcity = $params['pcity'];
        $padd = $params['padd'];
        $agent = new Reale_Model_Users_Agent($params);
        $first_name = $params['first_name'];
        $middle_name = $params['middle_name'];
        $last_name = $params['last_name'];
        $agent->setAgent_name("$first_name $middle_name $last_name");
        if ($husband_name) {
            $agent->setHusband_name($husband_name);
        }
        $paddress = new Reale_Model_Location();
        $paddress->setAddressline($padd)
            ->setPlace($pcity)
            ->setDistrict($pdistrict)
            ->setState($pstate)
            ->setPostalcode($ppostalcode);
        $agent->setPaddress($paddress);
        if ($params['is_cp_same'] == false) {
            $caddress = new Reale_Model_Location();
            $caddress->setAddressline($params['cadd'])
                ->setPlace($params['ccity'])
                ->setDistrict($params['cdistrict'])
                ->setState($params['cstate'])
                ->setPostalcode($params['cpostalcode']);
            $agent->setCaddress($caddress);
        }
        $nominee = new Reale_Model_Users_Nominee();
        $nominee_name = $params['nominee_name'];
        $nominee_relation = $params['nominee_relation'];
        $nominee_pan = $params['nominee_pan'];
        $naddress = new Reale_Model_Location();
        if ($params['is_np_same'] == false) {
            $naddress->setAddressline($params['nadd'])
                ->setPlace($params['ncity'])
                ->setDistrict($params['ndistrict'])
                ->setState($params['nstate'])
                ->setPostalcode($params['npostalcode']);
            $nominee->setAddress($naddress);
        } else {
            $nominee->setAddress($paddress);
        }
        $nominee->setNominee_name($nominee_name)
            ->setRelation($nominee_relation)
            ->setPan($nominee_pan);
        $agent->setNominee($nominee);
        $oper = strtolower($params['oper']);
        switch ($oper) {
            case 'add':
                $app_no = $params['app_no'];
                $agent_id = $app_no;
                $sponsor_id = $params['sponsor_id'];
                $rgrnDateobj = new Zend_Date(null, 'dd-MM-yyyy');
                $rgrn_date = $rgrnDateobj->toString('YYYY-MM-dd');
                $agent->setRgrn_date($rgrn_date)->setAgent_id($agent_id);
                $sponser = new Reale_Model_Users_Agent(array('agent_id' => $sponsor_id));
                $agent->setSponser($sponser);
                if ($agent->save()) {
                    echo $agent->getAgent_name() .
                     ' is now registered as agent, with agent id: ' .
                     $agent_id;
                    return;
                }
                break;
            default:
                throw new Zend_Exception('"' . $oper . '" is unknown operation.', 
                Zend_Log::ERR);
                break;
        }
        throw new Zend_Exception(
        'Operation on agent "' . $agent->getUsername() . '" not successfull.', 
        Zend_Log::ERR);
    }
    public function checkuidAction ()
    {
        // action body
    }
    public function cansponserAction ()
    {
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $this->_helper->layout()->disableLayout();
        $request = $this->getRequest();
        $agent_id = $request->getParam('agent_id');
        $agent = new Reale_Model_Users_Agent(array('agent_id' => $agent_id));
        $sponsered = $agent->canSponser();
        if (false === $sponsered) {
            throw new Zend_Exception('Can not sponser anymore.', Zend_Log::ERR);
        } else {
            echo 'Agents sponsered yet :' . $sponsered;
        }
    }
}

